﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class CmpAddUI : Form
    {
        public CmpAddUI()
        {
            InitializeComponent();
            //Init Data
            DataTable dtResult = DBSession.ExecuteDataTable("SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company ORDER BY CreateDate DESC", null);
            dgvCmpInfo.DataSource = dtResult;
        }

        private void btnAddCmp_Click(object sender, EventArgs e)
        {
            String EmpId = txtCmpId.Text.Trim().ToString();
            String EmpName = txtCmpName.Text.Trim().ToString();
            String Owner = txtOwner.Text.Trim().ToString();
            String EmpNumber = txtCmpNumber.Text.Trim().ToString();
            String Contactor = txtContacter.Text.Trim().ToString();
            String ContactorPhone = txtContactorPhone.Text.Trim().ToString();
            String Contractor = txtContractor.Text.Trim().ToString();
            String Remark = txtRemark.Text.Trim().ToString();
            String CreateDate = DateTime.Now.ToString();
            if (EmpId.Length > 0 && EmpName.Length > 0 && Owner.Length > 0 && Contactor.Length > 0 && ContactorPhone.Length > 0 && Contractor.Length > 0)
            {
                DataTable dt = DBSession.ExecuteDataTable("SELECT * FROM Company WHERE CmpId='"+EmpId+"'",null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    MessageBox.Show("已经有相同的企业ID存在，请检查你要新加入的企业ID号！");
                }
                else
                {
                    String sql = " INSERT INTO Company VALUES('"+EmpId+"','"+EmpName+"','"+Owner+"','"+EmpNumber+"','"+Contactor+"','"+ContactorPhone+"','"+Contractor+"','"+CreateDate+"','"+Remark+"');";
                    int count = DBSession.ExecuteNonQuery(sql, null);
                    if (count > 0)
                    {
                        DataTable dtResult = DBSession.ExecuteDataTable("SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company ORDER BY CreateDate DESC", null);
                        dgvCmpInfo.DataSource = dtResult;
                    }

                    //clear control value.
                    txtCmpId.Text = "";
                    txtCmpName.Text = "";
                    txtOwner.Text = "";
                    txtCmpNumber.Text = "";
                    txtContacter.Text = "";
                    txtContactorPhone.Text = "";
                    txtContractor.Text = "";
                    txtRemark.Text = "";
                    txtCmpId.Focus();
                }
            }
            else
            {
                MessageBox.Show("除备注外，其余信息不能为空！");
            }
        }
    }
}
